package com.xca.znb.view;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.bumptech.glide.Glide;
import com.xca.znb.R;
import com.xca.znb.adapter.SearchAdapter;
import com.xca.znb.entity.Foot;
import com.xca.znb.entity.Goods;
import com.xca.znb.model.impl.FootImpl;
import com.xca.znb.presenter.FootPresenter;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

public class FootprintActivity extends AppCompatActivity {
    private ImageButton footprintBackBtn ;
    private ListView listView;
    private ArrayAdapter<Goods> adapter ;
    private List<Foot> footList = new ArrayList<>();
    private List<Goods> goodsList = new ArrayList<>();
    private FootPresenter presenter ;
    @SuppressLint("MissingInflatedId")
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.footprint);
        footprintBackBtn = findViewById(R.id.footprint_back_btn);
        listView = findViewById(R.id.footPrint_list);

        FootImpl footImpl = new FootImpl(this);
        presenter = new FootPresenter(footImpl);
        SharedPreferences sharedPreferences = getSharedPreferences("loginPhone", Context.MODE_PRIVATE);
        String phone = sharedPreferences.getString("phone" , "");
        LocalDateTime nowTime = LocalDateTime.now();
        String secTime = nowTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));

        footList = presenter.getFoots(phone);

        for (Foot foot : footList){
            Goods goods = presenter.getGoodsById(foot.getGoodsId());
            if (goods != null){
                goodsList.add(goods);
            }
        }

        if (goodsList != null){
            setAdapterFoot();
            listView.setAdapter(adapter);
            adapter.notifyDataSetChanged();
        }
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                TextView showId = view.findViewById(R.id.searchItem_goods_show_goodsId) ;
                String sId = showId.getText().toString();

                int goodsId = Integer.parseInt(sId);
                Intent intent = new Intent(FootprintActivity.this , GoodsDetailActivity.class) ;
                intent.putExtra("goodsId" , sId) ;
                presenter.addOrUpdateFoot(goodsId,phone,secTime);
                startActivity(intent);
            }
        });
        footprintBackBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
    }
    private void setAdapterFoot(){
        adapter = new ArrayAdapter<Goods>(this , R.layout.search_list_item ,R.id.searchItem_show_goods_name , goodsList){
            public View getView(int position , View converView , ViewGroup parents){
                View view = super.getView(position, converView, parents);
                ImageView showImg = view.findViewById(R.id.searchItem_goods_img_show);
                TextView showName = view.findViewById(R.id.searchItem_show_goods_name);
                TextView showId = view.findViewById(R.id.searchItem_goods_show_goodsId);
                TextView showLocal = view.findViewById(R.id.searchItem_show_goods_local);
                TextView showPrice = view.findViewById(R.id.searchItem_show_goods_price);
                TextView showUnit = view.findViewById(R.id.searchItem_show_goods_unit);
                Goods goods = goodsList.get(position);
                if (goods != null){
                    Glide.with(FootprintActivity.this)
                            .load(goods.getFirst_image())
                            .into(showImg);
                    showName.setText(goods.getName());
                    showId.setText(String.valueOf(goods.getId()));
                    showLocal.setText(goods.getAddress());
                    showPrice.setText(String.valueOf(goods.getPrice()));
                    showUnit.setText(String.valueOf(goods.getUnit()));

                }
                return view ;
            }
        };
    }
}
